
let regex = {
    q1: /(.{0,25}#){8}(all|ALL)/,
    q2: /(.{0,25}#){6}(all|ALL)/,
    q3: /[a-zA-Z0-9]{5,25}/,
}

/**
 *  初始化PDA扫码
 * @param callback 扫码结果处理函数
 * @returns {boolean} 初始化成功返回true，否则返回false
 */
export function initScan(callback) {
    let barcodeModel = uni.requireNativePlugin("iData-BarcodePlugin-BarcodeModule");

    // 初始化扫描
    barcodeModel.initScan((ret) => {
        if (ret.code === 'success') {
            console.log("初始化成功");
        }
    });

    // 设置输出模式
    barcodeModel.setOutputMode(4, (ret) => {
        if (ret.code === 'success') {
            console.log("模式设置成功");
        }
    });

    // 获取全局事件监听插件
    let globalEvent = uni.requireNativePlugin('globalEvent');

    // 定义事件处理函数
    if (typeof callback === 'function') {
        callback();
    } else {
        console.error('initScan: 参数必须是函数类型');
    }

    // 添加条码事件监听
    globalEvent.addEventListener('iDataBarcodeEvent', callback);
    return true;
}

/**
 * 处理扫码结果
 * @param res 扫码结果
 * @returns {{type: number, invCode: null, batchNo: null, orderNo: null, barCode: *}}
 */
export function dealBarCde(res) {
    if (!res || !res.barcode) {
        return null;
    }
    let code = res.barcode;

    // 确保 code 是字符串类型
    if (typeof code !== 'string') {
        console.error('Invalid code type:', typeof code, code);
        return null;
    }

    let result = {
        type: 0,
        invCode: null,
        batchNo: null,
        orderNo: null,
        barCode: code,
        sucFlag: false,
    }
    if (regex.q1.test(code)) {
        let parts = code.split('#');
        result.type = 1;
        result.invCode = parts[1];
        result.batchNo = parts[2];
        result.orderNo = parts[6];
        result.sucFlag = true;
    } else if (regex.q2.test(code)) {
        let parts = code.split('#');
        result.type = 2;
        result.invCode = parts[0];
        result.batchNo = parts[1];
        result.orderNo = parts[3];
        result.sucFlag = true;
    }  else if (regex.q3.test(code)) {
        result.type = 3;
        result.invCode = code;
        result.sucFlag = true;
    }
    return result;
}

export function dealCamQr(res) {
    if (!res || !res.result) {
        return null;
    }
    let code = res.result;

    // 确保 code 是字符串类型
    if (typeof code !== 'string') {
        console.error('Invalid code type:', typeof code, code);
        return null;
    }

    let result = {
        type: 0,
        invCode: null,
        batchNo: null,
        orderNo: null,
        barCode: code,
        sucFlag: false,
    }
    if (regex.q1.test(code)) {
        let parts = code.split('#');
        result.type = 1;
        result.invCode = parts[1];
        result.batchNo = parts[2];
        result.orderNo = parts[6];
        result.sucFlag = true;
    } else if (regex.q2.test(code)) {
        let parts = code.split('#');
        result.type = 2;
        result.invCode = parts[0];
        result.batchNo = parts[1];
        result.orderNo = parts[3];
        result.sucFlag = true;
    }  else if (regex.q3.test(code)) {
        result.type = 3;
        result.invCode = code;
        result.sucFlag = true;
    }
    return result;
}



/**
 * 销毁PDA扫码
 * @param handlePdaScan 扫码处理函数
 */
export function destroyScan(handlePdaScan) {
    let globalEvent = uni.requireNativePlugin('globalEvent');
    globalEvent.removeEventListener('iDataBarcodeEvent', handlePdaScan);
}
